﻿Shader "My/Sprite"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Color", Color) = (1, 1, 1, 1)
        _Cutoff ("Alpha Cutoff", Range(0,1)) = 0.5
        _StencilRef ("StencilRef", Int) = 1
    }
    SubShader
    {
        Tags { "RenderType" = "Transparent" }
        
        Blend SrcAlpha OneMinusSrcAlpha
            ZWrite On
            ZTest LEqual

        Pass
        {
            Stencil
            {
                Ref [_StencilRef]
                Comp Equal
                Pass Keep
                Fail Keep
                ZFail Keep
            }
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite On
            ZTest LEqual

            // 顶点和片段着色器代码
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            fixed4 _Color;
            float _Cutoff;

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                fixed4 color : COLOR;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                fixed4 color : COLOR;
                float4 vertex : SV_POSITION;
            };

            v2f vert(appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.color = v.color * _Color; // 使用颜色
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 texColor = tex2D(_MainTex, i.uv); // 计算最终颜色
                if(i.color.r < 1 || i.color.g < 1 || i.color.b < 1){
                    texColor *= i.color;
                }
                //clip(texColor.a - _Cutoff);
                return texColor;
            }
            ENDCG
        }
    }
    FallBack "Sprites/Default"

}
